<template>
        <div class="list" ref="wrapper">
            <div>
                <div class="area">
                  <div class="title border-topbottom">当前城市</div>
                  <div class="buttonList">
                    <div class="button-wrapper">
                        <div class="button">{{city}}</div>
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="buttonList">
                    <div class="button-wrapper" v-for="item in hotCitiesList" :key="item.id">
                        <div class="button" @click="change(item.name)">{{item.name}}</div>
                    </div>
                </div>
            </div>
            <div class="area" v-for="(item, key) of cities" :key="key" :ref="el => elements[key] = el">
                <div class="title border-topbottom">{{key}}</div>
                <div class="item border-bottom" v-for="i in cities[key]" :key="i.id" @click="change(i.name)">{{i.name}}</div>
            </div>
            </div>
        </div>
</template>
<script>
import BScroll from 'better-scroll'
import { watch, onMounted, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CityList',
  props: {
    'hotCitiesList': {
      type: Array
    },
    'cities': {
      type: Object
    },
    'letter': {
      type: String
    },
    'alphabet': {
      type: Array
    }
  },
  setup(props){
    let scroll = null
    let store = useStore()
    let city = store.state.city
    let router = useRouter()
  //   let currentCity = ref(null)
    let wrapper = ref(null)
    let elements = ref({})

    onMounted(()=>{
      scroll = new BScroll(wrapper.value, {
        click: true
      })
    })

    watch(()=>{return props.letter}, (newValue, oldValue)=>{
      let ele = elements.value[newValue]
      scroll.scrollToElement(elements.value[newValue])
    })

    function change(data) {
      store.commit('changeCity', data)
      router.push('/')
    }
    return { city, wrapper, elements, change }
  }
}
</script>
<style lang="stylus" scoped>
    .border-topbottom
        &:before
            border-color #ccc
        &:after
            border-color #ccc
    .border-bottom
        &:after
            border-color #ccc
    .list
        overflow hidden
        position absolute
        top 1.58rem
        bottom 0
        left 0
        right 0
        .title
            line-height .54rem
            background-color #eee
            padding-left .2rem
            color #666
            font-size .26rem
        .buttonList
            padding .1rem .6rem .1rem .1rem
            overflow hidden
            .button-wrapper
                width 33.33%
                float left
                .button
                    margin .1rem
                    padding .1rem 0
                    border .02rem solid #ccc
                    border-radius .06rem
                    text-align center
        .item
            line-height .76rem
            padding-left .2rem
</style>
